package com.wangsd.web.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.report.entity.Manufacturer;
import com.wangsd.web.report.mapper.ManufacturerMapper;
import com.wangsd.web.report.service.ManufacturerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-10-15
 */
@Service
public class ManufacturerServiceImpl extends ServiceImpl<ManufacturerMapper, Manufacturer> implements ManufacturerService {

    @Resource
    ManufacturerMapper manufacturerMapper;

    @Override
    public IPage<Manufacturer> selectList(Integer pageNum, Integer pageSize, Manufacturer params, String code) {
        Page<Manufacturer> page = new Page<>(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(params.getName())){
            queryWrapper.like("name",params.getName());
        }
        if(StringUtils.isNotBlank(code)){
            queryWrapper.likeRight("code",code);
        }
        if(StringUtils.isNotBlank(params.getCode())){
            queryWrapper.likeRight("code",params.getCode());
        }
        queryWrapper.eq("deleted","0");
        return manufacturerMapper.selectPage(page,queryWrapper);
    }

    @Override
    public void addOrUpdate(Manufacturer params) {
        if (params.getId() != null) {
            manufacturerMapper.updateById(params);
        } else {
            manufacturerMapper.insert(params);
            //保存检索CODE
            params.setCode(params.getId() + "");
            manufacturerMapper.updateById(params);
        }
    }

    @Override
    public List<Map> selectAllByUserCode(String code) {
        List<Map> resultList = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        queryWrapper.eq("deleted","0");
        List<Manufacturer> list = manufacturerMapper.selectList(queryWrapper);
        for (Manufacturer sb : list) {
            Map map = new HashMap();
            map.put("id", sb.getId());
            map.put("name", sb.getName());
            map.put("code", sb.getCode());
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public List<Manufacturer> selectAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("deleted","0");
        List<Manufacturer> list = manufacturerMapper.selectList(queryWrapper);
        return list;
    }
}
